#pragma once
#include "Runtime/BattleFirePrefix.h"

namespace Alice{
    class WindowsRWLock
    {
    public:
        WindowsRWLock(const WindowsRWLock&) = delete;
        WindowsRWLock& operator=(const WindowsRWLock&) = delete;

        FORCEINLINE WindowsRWLock(uint32 Level = 0)
        {
            InitializeSRWLock(&mMutex);
        }

        ~WindowsRWLock()
        {
        }

        FORCEINLINE void ReadLock()
        {
            AcquireSRWLockShared(&mMutex);
        }

        FORCEINLINE void WriteLock()
        {
            AcquireSRWLockExclusive(&mMutex);
        }

        FORCEINLINE bool TryReadLock()
        {
            return !!TryAcquireSRWLockShared(&mMutex);
        }

        FORCEINLINE bool TryWriteLock()
        {
            return !!TryAcquireSRWLockExclusive(&mMutex);
        }

        FORCEINLINE void ReadUnlock()
        {
            ReleaseSRWLockShared(&mMutex);
        }

        FORCEINLINE void WriteUnlock()
        {
            ReleaseSRWLockExclusive(&mMutex);
        }

    private:

        bool IsLocked()
        {
            if (TryAcquireSRWLockExclusive(&mMutex))
            {
                ReleaseSRWLockExclusive(&mMutex);
                return false;
            }
            else
            {
                return true;
            }
        }
        SRWLOCK mMutex;
    };
    typedef WindowsRWLock RWLock;
}